package com.company.test16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Administrator on 2016.8.3.
 */
public class Test1604 extends JFrame {

    private Show show;
    private Butten butten;

    public Test1604(){
        show=new Show();
        butten=new Butten();
        this.add(show,BorderLayout.CENTER);
        this.add(butten,BorderLayout.SOUTH);
    }

    public static void main(String[] args){
        Test1604 frame=new Test1604();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Welcome to JAVA");
        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private class Show extends JPanel{
        final JLabel number1=new JLabel("Number1");
        final JLabel number2=new JLabel("Number2");
        final JLabel result=new JLabel("Result");

        JTextField textField1=new JTextField(8);
        JTextField textField2=new JTextField(8);
        JTextField textFieldResult=new JTextField(8);

        public Show(){
            this.setLayout(new FlowLayout());
            this.add(number1);
            this.add(textField1);
            this.add(number2);
            this.add(textField2);
            this.add(result);
            this.add(textFieldResult);
        }
    }

    private class Butten extends JPanel{
        final JButton addjbt=new JButton("Add");
        final JButton subtractjbt=new JButton("subtract");
        final JButton multiptyjbt=new JButton("Multiply");
        final JButton dividejbt =new JButton("Divide");

        public Butten(){
            addjbt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    double n1=Double.valueOf(Test1604.this.show.textField1.getText());
                    double n2=Double.valueOf(Test1604.this.show.textField2.getText());

                    Test1604.this.show.textFieldResult.setText(n1+n2+"");
                }
            });

            subtractjbt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    double n1=Double.valueOf(Test1604.this.show.textField1.getText());
                    double n2=Double.valueOf(Test1604.this.show.textField2.getText());

                    Test1604.this.show.textFieldResult.setText((n1-n2)+"");
                }
            });

            multiptyjbt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    double n1=Double.valueOf(Test1604.this.show.textField1.getText());
                    double n2=Double.valueOf(Test1604.this.show.textField2.getText());

                    Test1604.this.show.textFieldResult.setText((n1*n2)+"");
                }
            });

            dividejbt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    double n1=Double.valueOf(Test1604.this.show.textField1.getText());
                    double n2=Double.valueOf(Test1604.this.show.textField2.getText());

                    Test1604.this.show.textFieldResult.setText((n1/n2)+"");
                }
            });

            this.setLayout(new FlowLayout());
            this.add(addjbt);
            this.add(subtractjbt);
            this.add(multiptyjbt);
            this.add(dividejbt);
        }
    }
}